﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using MyVideoViewerLibrary;

using Microsoft.MediaCenter.UI;

namespace MyVideoViewerMCPlugin
{
    public class Trailer
    {
        string[] extensions = new string[] { ".mov", ".avi", ".mpeg", ".mpg", ".mkv", ".wmv", ".mp4", ".dvr-ms" };
        private BooleanChoice _checkBoxItem = new BooleanChoice();
        public BooleanChoice CheckBoxItem
        {
            get
            {
                if (_checkBoxItem == null)
                {
                    _checkBoxItem = new BooleanChoice();

                }
                return _checkBoxItem;
            }
            set
            {
                _checkBoxItem = value;
            }
        }

        public MovieTrailer MovieTrailer;

        public string Title { get { return MovieTrailer.Title; } }
        public string Runtime { get { return MovieTrailer.Runtime; } }
        public string Studio
        {
            get
            {
                if (MovieTrailer.Studio == null)
                    return "";
                else
                    return MovieTrailer.Studio.Name;
            }
        }
        public string Director
        {
            get
            {
                if (MovieTrailer.Director == null)
                    return "";
                else
                    return MovieTrailer.Director.PersonName;
            }
        }
        public string Rating
        {
            get
            {
                if (MovieTrailer.Rating == null)
                    return "";
                else
                    return MovieTrailer.Rating.RatingDesc;
            }
        }
        public string Description { get { return MovieTrailer.Description; } }
        public string GenreString
        {
            get
            {
                return string.Join(",", MovieTrailer.MovieTrailer_Genres.Select(g => g.Genre.GenreDesc).ToArray());
            }
        }
        public List<MediaFile> Files { get; set; }
        public List<CastMember> Cast { get; set; }
        public Image Image { get; set; }
        public bool ShowBackgroundImage
        {
            get
            {
                string imageLocation = Path.Combine(MovieTrailer.MovieFolder, "fanart.jpg");
                return File.Exists(imageLocation);
            }
        }
        public Image BackgroundImage
        {
            get
            {
                string imageLocation = Path.Combine(MovieTrailer.MovieFolder, "fanart.jpg");
                if (File.Exists(imageLocation))
                    return new Image("file://" + imageLocation);
                else
                    return new Image("resx://MyVideoViewerMCPlugin/MyVideoViewerMCPlugin.Resources/clear");
            }
        }

        public Trailer() { }
        public Trailer(MovieTrailer movieTrailer, System.Data.Linq.EntitySet<MovieTrailer_Genre> genres, System.Data.Linq.EntitySet<MovieTrailer_Person> persons)
        {
            MovieTrailer = movieTrailer;

            CheckBoxItem.Description = "Watched";
            CheckBoxItem.Value = MovieTrailer.Watched;
            CheckBoxItem.PropertyChanged += new PropertyChangedEventHandler(CheckBoxItem_PropertyChanged);
            MovieTrailer.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Trailer_PropertyChanged);
        }

        void Trailer_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Watched")
            {
                MyVideoViewerBusiness.VideoViewerContext.SubmitChanges();
            }
                //MyVideoViewerBusiness.MarkTrailerAsWatched(MovieTrailer, MovieTrailer.Watched);
        }

        public void GetFiles()
        {
            Files = new List<MediaFile>();
            DirectoryInfo folder = new DirectoryInfo(MovieTrailer.MovieFolder);

            foreach (FileInfo file in folder.GetFiles().OrderBy(f => f.LastWriteTime))
            {
                if (extensions.Contains(file.Extension))
                    Files.Add(new MediaFile() { Path = file.FullName }); 
            }

            if (Files.Count == 1)
                Files[0].FileName = "Watch";
            else
            {
                for (int i = 0; i < Files.Count; i++)
                {
                    Files[i].FileName = string.Format("Trailer {0}", i + 1);
                }
            }
        }

        void CheckBoxItem_PropertyChanged(IPropertyObject sender, string property)
        {
            if (property == "Value")
            {
                MovieTrailer.Watched = CheckBoxItem.Value;
            }
        }
    }
}
